package jp.gr.java_conf.ionexux.feztimer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CrystalFragment extends Fragment implements OnClickListener {

	private final String TAG = "FragmentCrystalFragment";

	private View mProgressBar1;
	private View mProgressBar2;
	private View mProgressBar3;
	private View mLinearLayoutLevelView;
	private View mTextViewCharacterLevel;
	private View mTextViewCurrentCrystal;
	private View mTextViewMaxCrystal;
	private View mButtonStart;
	private View mButtonStop;

	private Handler mHandler = new Handler();
	private MediaPlayer mPlayer;
	private Timer mTimerCrystalCounterView;
	private Timer mTimerCrystalBackGround;
	private SharedPreferences mSharedPreferences;
	private SharedPreferences.Editor mEditor;
	private Context mContext;
	private int ringTone;
	private boolean mCrystalFlag = false;
	private int cycleRingToneLapTIme;

	private final Integer MAXFIELDOUTTIME = 60 * 5 * 1000;
	private Integer maxCrystalTime = (4 * 12 + 10 * 8) * 1000;
	private Integer bestCrystalTime = 4 * 12 * 1000;

	private int currentTime;
	private Integer mCharacterLevel;
	private int progressLapTime;

	@Override
	public void onAttach(Activity act) {
		super.onAttach(act);
		Log.d(TAG, "onAttach");
		mContext = getActivity();

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");

		// 設定情報のインスタンス化
		mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(mContext);
		mEditor = mSharedPreferences.edit();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");

		// レイアウトを生成
		View v = inflater.inflate(R.layout.crystal, container, false);

		// 各種Viewを生成
		mProgressBar1 = v.findViewById(R.id.crystalProgressBar1);
		mProgressBar2 = v.findViewById(R.id.crystalProgressBar2);
		mProgressBar3 = v.findViewById(R.id.crystalProgressBar3);
		mLinearLayoutLevelView = v.findViewById(R.id.crystalLevelLinearLayout);
		mTextViewCharacterLevel = v.findViewById(R.id.crystalLevelTextView);
		mTextViewCurrentCrystal = v.findViewById(R.id.crystalCurrentTextView);
		mTextViewMaxCrystal = v.findViewById(R.id.crystalMaxTextView);
		mButtonStart = v.findViewById(R.id.crystalStartButton);
		mButtonStop = v.findViewById(R.id.crystalStopButton);

		// クリックリスナーを設定
		((LinearLayout) mLinearLayoutLevelView).setOnClickListener(this);
		((TextView) mTextViewCharacterLevel).setOnClickListener(this);
		((TextView) mTextViewCurrentCrystal).setOnClickListener(this);
		((TextView) mTextViewMaxCrystal).setOnClickListener(this);
		((Button) mButtonStart).setOnClickListener(this);
		((Button) mButtonStop).setOnClickListener(this);

		((ProgressBar) mProgressBar1).setMax(bestCrystalTime);
		((ProgressBar) mProgressBar2).setMax(maxCrystalTime);
		((ProgressBar) mProgressBar3).setMax(MAXFIELDOUTTIME);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle bundle) {
		super.onActivityCreated(bundle);
		Log.d(TAG, "onActivityCreated");
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		mCharacterLevel = mSharedPreferences
				.getInt("crystalCharacterLevel", 20);
		((TextView) mTextViewCharacterLevel)
				.setText(mCharacterLevel.toString());

		ringTone = R.raw.powercounter;
		cycleRingToneLapTIme = 10000;
		progressLapTime = 500;

	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");

		mEditor.putInt("crystalCharacterLevel", mCharacterLevel);
		mEditor.commit();

	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "onDestroyView");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "onDetach");
	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.crystalLevelLinearLayout:

			break;

		case R.id.crystalStartButton:
			startCounter();
			break;

		case R.id.crystalStopButton:
			stopCounter();
			break;

		default:
			break;
		}
	}

	private void startCounter() {
		if (mCrystalFlag)
			stopCounter();
		playSound(ringTone);
		mCrystalFlag = true;
		mTimerCrystalCounterView = new Timer();
		mTimerCrystalCounterView.schedule(new TimerTask() {
			@Override
			public void run() {
				currentTime = currentTime + progressLapTime;
				mHandler.post(new Runnable() {
					public void run() {
						((ProgressBar) mProgressBar1).setProgress(currentTime);
						((ProgressBar) mProgressBar2).setProgress(currentTime);
						((ProgressBar) mProgressBar3).setProgress(currentTime);
					}
				});
			}
		}, 0, progressLapTime);

		mTimerCrystalBackGround = new Timer();
		mTimerCrystalBackGround.schedule(new TimerTask() {
			@Override
			public void run() {
				playSound(ringTone);
			}
		}, bestCrystalTime, cycleRingToneLapTIme);

	}

	private void stopCounter() {
		if (mCrystalFlag) {
			mCrystalFlag = false;

			mTimerCrystalBackGround.cancel();
			mTimerCrystalBackGround = null;

			mTimerCrystalCounterView.cancel();
			mTimerCrystalCounterView = null;

			((ProgressBar) mProgressBar1).setProgress(0);
			((ProgressBar) mProgressBar2).setProgress(0);
			((ProgressBar) mProgressBar3).setProgress(0);
			currentTime = 0;
		}
	}

	public void playSound(int ringTone) {
		stopSound();
		mPlayer = MediaPlayer.create(mContext, ringTone);
		mPlayer.seekTo(0);
		mPlayer.start();
		mPlayer.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer mp) {
				stopSound();
			}
		});

	}

	public void stopSound() {
		if (mPlayer == null)
			return;
		mPlayer.stop();
		mPlayer.setOnCompletionListener(null);
		mPlayer.release();
		mPlayer = null;
	}
}
